<template>
	<view class="content">
		<view v-if="groupusers.length > 0"
			style="width: 100%;display: flex;align-items: center;background: white;padding: 15rpx;flex-wrap: wrap;border-bottom: 1rpx solid #d9d9d955;">
			<u-avatar v-for="(item,k) in groupusers" :src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.fid + '.jpg'" size="80" style="margin: 10rpx;"
				mode="square"></u-avatar>
		</view>
		
		<view v-else
			style="width: 100%;display: flex;align-items: center;background: white;padding: 35rpx 15rpx;flex-wrap: wrap;border-bottom: 1rpx solid #d9d9d955;color: #959595;">
			选择群成员
		</view>

		<scroll-view style="width: 100%;flex: 1;overflow-y: auto;flex-direction: column;background: #fff;" scroll-y
			:scroll-into-view="scrollTop">
				<view v-for="(item, index) in list" :key="index">
					<view style="margin: 15rpx;" :id="item.letter">
						{{item.letter}}
					</view>
					<view style="width: 100%;display: flex;flex-direction: column;">
						<view @click="clickuser(itemx)" v-for="(itemx,ain) in item.list" class="list-cell1"
							style="padding: 15rpx;display: flex;align-items: center;justify-content: space-between;">
							<view style="display: flex;align-items: center;">
								<u-avatar mode="square" size="80"
									:src="$store.state.common.systemInfo.fileurl + 'avatar/' + itemx.fid + '.jpg'"></u-avatar>
								<view
									style="margin-left: 10rpx;display: flex;flex-direction: column;justify-content: center;">
									<view style="font-size: 30rpx;">
										{{itemx.name}}
									</view>
									<view v-if="itemx.note" style="color: #a6a6a6;font-size: 24rpx;">
										{{itemx.note}}
									</view>
								</view>
							</view>
							<view class="">
								<image v-if="haveue(itemx)" src="/static/images/checked.png" style="width: 40rpx;height: 40rpx;margin-right: 10rpx;" mode=""></image>
								<image v-else src="/static/images/check.png" style="width: 40rpx;height: 40rpx;margin-right: 10rpx;" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			<view
				style="position: fixed;right: 20rpx;top: 25%;display: flex;flex-direction: column;align-items: center;font-size: 24rpx;">
				<view v-for="(item, index) in list" :key="index">
					<view @click="scrollTop = item.letter" class="" :id="item.letter">
						{{item.letter}}
					</view>
				</view>
			</view>
		</scroll-view>


		<view
			style="display: flex;width: 100%;align-items: center;position: fixed;bottom: 0rpx;left: 0rpx;height: 120rpx;background: #e8eaf0;justify-content: flex-end;">
			<view v-if="groupusers.length < 2" class="bgyxtag"
				style="margin-right: 42rpx;background: #1aaaaa88;color: white;padding: 16rpx;font-size: 26rpx;border-radius: 10rpx;">
				建立群聊
			</view>
			<view @click="makegroup" v-else class="bgyxtag"
				style="margin-right: 42rpx;background: #1aaaaa;color: white;padding: 16rpx;font-size: 26rpx;border-radius: 10rpx;">
				建立群聊({{groupusers.length}})
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		pub
	} from '../../common/event';
	export default {
		data() {
			return {
				scrollTop: '',
				groupusers: [],
				grids:[]
			}
		},
		components: {

		},
		computed: {
			...mapState({
				list: (state) => {
					const ulist = state.friend.list.filter(e => e.gid == 0 && e.status == "正常").sort((a, b) => (a.letter)
						.localeCompare(b.letter))
					const ilist = {}
					for (let o of ulist) {
						if (ilist[o.letter]) {
							ilist[o.letter].push(o)
						} else {
							ilist[o.letter] = [o]
						}
					}
					const datalist = []
					for (let k in ilist) {
						datalist.push({
							letter: k,
							list: ilist[k]
						})
					}
					return datalist
				}
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onLoad() {
			
		},
		methods: {
			clickuser:function(e){
				if(this.grids.includes(e.id)){
					this.removeusercs(e)
				}else{
					this.changeusercs(e)
				}
			},
			changeusercs: function(e) {
				console.log(e)
				this.groupusers.push(e)
				console.log(this.groupusers)
				this.grids = this.groupusers.map(e=>e.id)
				console.log(this.grids)
			},
			removeusercs: function(e) {
				console.log(e)
				const arr = []
				for(let o of this.groupusers){
					if(o.id != e.id){
						arr.push(o)
					}
				}
				this.groupusers = arr
				this.grids = this.groupusers.map(e=>e.id)
			},
			haveue: function(e){
				return this.grids.includes(e.id)
			},
			makegroup: async function(){
				uni.showLoading({
					title:"建立群聊..."
				})
				const fids = this.groupusers.map(e=>{
					return {
						id:e.fid,
						name:e.name
					}
				})
				const data = fids
				const ret = await this.$u.post(`friend/friend/makegroup`,data)
				if(ret && ret.success){
					uni.hideLoading()
				}
				
				uni.hideLoading()
				uni.switchTab({
					url:"/pages/index/index"
				})
			},
		}
	}
</script>


<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
		padding-bottom: 120rpx;
	}
</style>